package com.hsgene.product.controller;

import com.hsgene.common.util.resolver.FromSession;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.web.session.UserInfo;
import com.hsgene.product.applets.dto.AppletsGoodsCommentInfoDto;
import com.hsgene.product.applets.dto.AppletsGoodsDetailsInfoDto;
import com.hsgene.product.applets.dto.GoodsCommentListInfoDto;
import com.hsgene.product.applets.goods.AppletsErrorCode;
import com.hsgene.product.service.AppletsGoodsService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序商品详情controller
 *
 * @author wxf
 * @date 2018/10/11 14:13
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing/commodities")
public class AppletsGoodsController {

    /**
     * Log
     */
    private static final Logger LOGGER = LogManager.getLogger(AppletsGoodsController.class);

    @Resource
    private AppletsGoodsService appletsGoodsService;

    /**
     * 获取所有评论
     *
     * @param goodsId 商品id
     * @param page    页数
     * @return
     */
    @GetMapping("{commodity_id}/comments")
    public ApiResult<Object> getAllCommitInfo(@PathVariable("commodity_id") String goodsId, Integer
            page, Integer size, Integer label) {
        try {
            if (StringUtils.isEmpty(goodsId)) {
                return ApiResult.fail(AppletsErrorCode.APPLETS_GOODS_ID_IS_NOT_NULL);
            }
            if (page == null || size == null || size == 0) {
                return ApiResult.fail(AppletsErrorCode.SIZA_IS_NOT_NULL);
            }
            if (page< 1){
                page = 1;
            }
            //label为空,说明获取的是全部列表
            if (label == null) {
                GoodsCommentListInfoDto commentInfoDtos = appletsGoodsService.getAllCommitInfo(goodsId, page, size);
                return new ApiResult(200, "success", "获取商品详情成功", commentInfoDtos);
            }
            if (label < 1 && label > 4) {
                return ApiResult.fail(AppletsErrorCode.LABEL_IS_ERROR);
            }
            List<AppletsGoodsCommentInfoDto> commentInfoDtos = appletsGoodsService.getLabelTypeCommit(goodsId, label,
                    page, size);
            return new ApiResult(200, "success", "获取商品详情成功", commentInfoDtos);

        } catch (Exception e) {
            LOGGER.error("get good commit is error", e);
            return ApiResult.fail(AppletsErrorCode.APPLETS_GOODS_COMMENT_IS_ERROR);
        }
    }

    /**
     * 获取商品优惠券
     *
     * @param discountId 优惠券id
     * @param userInfo   用户
     * @return
     */
    @PostMapping("discounts/{discount_id}")
    public ApiResult<String> getGoodsDiscount(@PathVariable("discount_id") String discountId, @FromSession UserInfo
            userInfo) {
        try {
            String userId = userInfo.getUserId();
            if (StringUtils.isEmpty(userInfo.getUserId())) {
                return ApiResult.fail(AppletsErrorCode.USER_INFO_IS_NOT_NULL);
            }
            if (StringUtils.isEmpty(discountId)) {
                return ApiResult.fail(AppletsErrorCode.DISCOUNT_INFO_IS_NOT_NULL);
            }
            String result = appletsGoodsService.getGoodsDiscount(discountId, userId);
            if (StringUtils.isEmpty(result)) {
                return new ApiResult(200, "success", "领取成功", null);
            }
            return ApiResult.fail(AppletsErrorCode.DISCOUNT_INFO_IS_EXISTS);
        } catch (Exception e) {
            LOGGER.error("add discount info is error...", e);
            return ApiResult.fail(AppletsErrorCode.ADD_DISCOUNT_IS_ERROR);
        }
    }
}
